<?php

/**
 * Layout Model
 * @author phuongtn
 */
class layout 
{
    /**
     * @var system_model Singleton instance
     */
    protected static $_instance;

    /**
     * Avoid directly create instance
     */
    private function __construct()
    {}

    /**
     * Retrieve singleton instance
     *
     * @return system_model
     */
    public static function getInstance()
    {
        if (null === self::$_instance)
        {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    /**
     * Reset the singleton instance
     *
     * @return void
     */
    public static function resetInstance()
    {
        self::$_instance = null;
    }

    
    /**
     * 
     * @param int $cateID
     * @param string $mapParams = array(
                'LayoutName'    => $layoutName,
                'SectionName'   => $sectionName,
                'Module'        => $module,
                'Controller'    => $controller,
                'Action'        => $action,
                'CateID'        => $cateID,
            );
     * @return type
     * @throws Core_Exception 
     */
    public function getLayoutFull($mapParams)
    {
        try
        {
            //init default response
            $response = array(
                'layout'    => '',
                'sections'  => '',
                'blocks'    => '',
                );
            
            //init model metadata
            $mapMeta = new Fpt_Business_Block_layout_metadata_maplayoutsection();
            //init data
            $mapMeta->init($mapParams);
            //get formatted data
            $formatParams = $mapMeta->getData();
            //get memcache key
            $memcaheKey = $formatParams['MapLayout'];
            //check if
            $memcacheInstance = Fpt_Global::getMemcacheInstance(0,ZONE_ENV, 'block1');
            
            $response = $memcacheInstance->read($memcaheKey);
            
            //$response = false;
            if($response == false)
            {
                if(!isset($mapParams['LayoutName']))
                {
                    //init model redis
                    $mapParams['LayoutName'] = $this->lookupMapLayout($formatParams);
                    
                    if(empty($mapParams['LayoutName'])){
                        return $response;
                    }
                }
               
                //get layout detail
                $layoutDetail = $this->getLayout($mapParams['LayoutName']);
                
                if(!empty($layoutDetail)){
                    $response['layout'] = str_replace('.phtml', '', $layoutDetail['LayoutView']);
                    $response['layoutName'] = $layoutDetail['LayoutView'];
                }
                
                //get all section
                $arrSection = $this->getMapLayoutSection($mapParams);
                
                //check have section
                if(is_array($arrSection['data']))
                {
                    //loop section
                    foreach($arrSection['data'] as $sectionOrder => $sectionName)
                    {
                        //init params
                        $mapBlockParams['MapLayoutSectionName'] = $arrSection['key'];
                        $mapBlockParams['SectionName']  = $sectionName;
                        $mapBlockParams['SectionOrder'] = $sectionOrder;
                        //get all blocks
                        $blockData = $this->getMapSectionBlock($mapBlockParams);
                        
                        //init data new
                        $blockDataNew = array('data'=>'');
                        if(is_array($blockData['data']))
                        {
                            foreach($blockData['data'] as $order => $params)
                            {
                                //decode params
                                $params = Zend_Json::decode($params);
                                //format data for front end
                                $blockDataNew['data'][$order]['blockParams'] = $params;
                                $blockDataNew['data'][$order]['blockName']   = $params['BlockName'];
                                $allBlockName[]  = $params['BlockName'];
                            }
//                            //modify arrdata for front end
//                            $sectionName = str_replace($mapParams['LayoutName'].'_', '', $sectionName);
                            //end modify arrdata 
                            //$response['sections'][$sectionName] = $blockDataNew['data'];
                            $response['sections'][$sectionName.'_'.$sectionOrder] = $blockDataNew['data'];
                        }//end check block data
                    }//end loop section
                }//end check get section
                
                if(is_array($allBlockName))
                {
                    //init model block
                    $modelBlock = block::getInstance();
                    
                    //loop block name to get detail
                    foreach($allBlockName as $blockName)
                    { 
                        //get block detail
                        $blockDetail = $modelBlock->getBlock($blockName);
                        
                        $response['blocks'][$blockName] = $blockDetail;
                    }
                    
                }
               
                //$memcacheInstance->write($memcaheKey, $response);
            }
            
            if(!empty($response['sections']) && is_array($response['sections']))
            {
                foreach($response['sections'] as $sectionName => $value)
                {
                    $lasPosition = strrpos($sectionName, '_');
                    $newSectionName = substr($sectionName, 0, $lasPosition);
                    
                    //$newSectionName = str_replace($response['layout'].'_', '', $newSectionName);
                    $response['sections'][$newSectionName] = $value;
                    unset($response['sections'][$sectionName]);
                    
                    //sort by block orther
                    if(!empty($response['sections'][$newSectionName])){
                        ksort($response['sections'][$newSectionName]);
                    }
                }
            }
            return $response;
        }
        catch(Exception $ex)
        {
            throw new Core_Exception($ex->getMessage(), $ex->getCode());
        }
    }
    
    
    public function lookupMapLayout($formatParams)
    {
        try
        {
            if(isset($formatParams['MapLayout']) )
            {
                $layoutRedis = Fpt_Business_Block_layout_adapter_redis::getInstance();
                $layoutName = $layoutRedis->lookupMapLayout($formatParams['MapLayout']);
            }
            
            if(empty($layoutName))
            {
                $layoutMsql = Fpt_Business_Block_layout_adapter_mysql::getInstance();
               
                $resultDB = $layoutMsql->lookupMapLayout($formatParams);
                
                if(!empty($resultDB)){
                    $layoutName = $resultDB[0]['LayoutName'];
                    $this->backupMap();
                }
            }
           
            return $layoutName;
        }
        catch(Exception $ex)
        {
            throw new Core_Exception($ex->getMessage(), $ex->getCode());
        }
          
    }
    
    /**
     * Add layout
     * @param type $params
     * @return type
     * @throws Core_Exception 
     */
    public function addLayout($layoutParams)
    {
        try
        {
            //init model metadata
            $layoutMeta = new Fpt_Business_Block_layout_metadata_layout();
            //init data
            $layoutMeta->init($layoutParams);
            //get formatted data
            $formatParams = $layoutMeta->getData();
            //unsert old
            unset($layoutParams);
            
            //read config 
            $config = Fpt_Global::getConfig('application');
            //init model db
            $modelSql = Fpt_Business_Block_layout::factory($config['block']['db']);
            //add db layout
            $insertRS = $modelSql->addLayout($formatParams);
           
            //if insert db success;
            if($insertRS !== false)
            {
                //init model redis
                $layoutRedis = Fpt_Business_Block_layout_adapter_redis::getInstance();
                $cacheFormat = $layoutMeta->getDataCache();
                $redis =  $layoutRedis->addLayout($cacheFormat);
                if($redis != false)
                {
                    return $cacheFormat['LayoutName'];
                }
            }
            return false;
        }
        catch(Exception $ex)
        {
            throw new Core_Exception($ex->getMessage(), $ex->getCode());
        }
        return false;
    }
    
    /**
     * Edit layout
     * @param type $params
     * @return type
     * @throws Core_Exception 
     */
    public function editLayout($layoutParams)
    {
        try
        {
            //init model metadata
            $layoutMeta = new Fpt_Business_Block_layout_metadata_layout();
            //init data
            $layoutMeta->init($layoutParams);
            //get formatted data
            $formatParams = $layoutMeta->getData();
            //unsert old
            unset($layoutParams);
            
            //read config 
            $config = Fpt_Global::getConfig('application');
            //init model db
            $modelSql = Fpt_Business_Block_layout::factory($config['block']['db']);
            //add db layout
            $insertRS = $modelSql->editLayout($formatParams);
           
           
            //if insert db success;
            if($insertRS !== false)
            {
                //init model redis
                $layoutRedis = Fpt_Business_Block_layout_adapter_redis::getInstance();
                $cacheFormat = $layoutMeta->getDataCache();
                $redis =  $layoutRedis->editLayout($cacheFormat);
                
                if($redis != false)
                {
                    return $cacheFormat['LayoutName'];
                }
            }
            return false;
        }
        catch(Exception $ex)
        {
            throw new Core_Exception($ex->getMessage(), $ex->getCode());
        }
        return false;
    }
    
    /**
     * 
     * @param int $cateID
     * @param string $controller
     * @return type
     * @throws Core_Exception 
     */
    public function getLayout($layoutName, $status = 1)
    {
        try
        {
            //init model redis
            $layoutRedis = Fpt_Business_Block_layout_adapter_redis::getInstance();
            //init params
            $getParams = array('LayoutName' => $layoutName, 'LayoutStatus' => $status);
            //get redis detail
            $layoutDetail =  $layoutRedis->getLayout($getParams);
            
            if(empty($layoutDetail))
            {
                //read config 
                $config = Fpt_Global::getConfig('application');
                //init model db
                $modelSql = Fpt_Business_Block_layout_adapter_mysql::getInstance();
                //add db layout
                $layoutDetail = $modelSql->getLayout($getParams);
               
                //if insert db success;
                if($layoutDetail !== false && !empty($layoutDetail))
                {
                    $layoutDetail = $layoutDetail[0];
                    $layoutRedis->addLayout($layoutDetail);
                }
            }
            return $layoutDetail;
        }
        catch(Exception $ex)
        {
            throw new Core_Exception($ex->getMessage(), $ex->getCode());
        }
    }
    
   /**
     * Get list layout
     * @return type
     * @throws Core_Exception 
     */
    public function getListLayout()
    {
        try
        {
            $response = false;
            //init model redis
            $layoutRedis = Fpt_Business_Block_layout_adapter_redis::getInstance();
          
            //get redis detail
            $layoutList =  $layoutRedis->getListLayout();
            
            
            if(empty($layoutList))
            {
                //read config 
                $config = Fpt_Global::getConfig('application');
                //init model db
                $modelSql = Fpt_Business_Block_layout::factory($config['block']['db']);
                //add db layout
                $layoutListDB = $modelSql->getListLayout();
                
                foreach($layoutListDB['data'] as $key => $detail)
                {
                    $layoutList[] = $detail['LayoutName'];
                }
            }
           
            
            //check get list sucess
            if(!empty($layoutList))
            {
                foreach($layoutList as $layoutName)
                {
                    
                    $response[] = $this->getLayout($layoutName);
                    
                }
                return $response;
            }
           
        }
        catch(Exception $ex)
        {
            throw new Core_Exception($ex->getMessage(), $ex->getCode());
        }
        return $response;
    }
    
    
    public function addLayoutSections($layoutName, $arrSections, $isUpdate = false)
    {
        try
        {

            $response = false;
            $modelSection = section::getInstance();
            foreach($arrSections as $sectionTitle => $sectionDesc)
            {    
                if(!empty($sectionTitle))
                {
                    $sectionParams = array(
                        'SectionTitle'  => $sectionTitle, 
                        'SectionName'   => Fpt_Global::covertVN($layoutName).'_'.Fpt_Global::covertVN($sectionTitle), 
                        'SectionDesc'   => $sectionDesc,
                        'LayoutName'    => $layoutName
                        );
                    $sectionName   = $sectionParams['SectionName'];

                    $addSection = $modelSection->addSection($sectionParams);

                    if($isUpdate == false)
                    {
                        $sectionName = $addSection['SectionName'];
                    }
                    $sections[] = $sectionName;
                }
            }
            if(!empty($sections))
            {
                
                 $layoutName = Fpt_Global::covertVN($layoutName);
                 //init model redis
                 $layoutRedis = Fpt_Business_Block_layout_adapter_redis::getInstance();
                 $response =  $layoutRedis->addLayoutSections($layoutName, $sections);
            }
            
            return $response;
        }
        catch(Exception $ex)
        {
            throw new Core_Exception($ex->getMessage(), $ex->getCode());
        }
        return false;
    }
    
    
    public function  getLayoutSections($layoutName)
    {
        try
        {
            //init model redis
            $layoutRedis = Fpt_Business_Block_layout_adapter_redis::getInstance();
            $response =  $layoutRedis->getLayoutSections($layoutName, $sections);
            if(empty($response))
            {
                //read config 
                $config = Fpt_Global::getConfig('application');
                //init model db
                $modelSql = Fpt_Business_Block_section::factory($config['block']['db']);
                //add db layout
                $responseDB = $modelSql->getListSection($layoutName);
                foreach($responseDB as $key => $listSectionDetail)
                {
                    
                    $response[] = $listSectionDetail['SectionName'];
                }
            }

            return $response;
        }
        catch(Exception $ex)
        {
            throw new Core_Exception($ex->getMessage(), $ex->getCode());
        }
        return false;
    }
    
    
     /**
     * Map layout with cate & controller
     * @param type $params
     * @return type
     * @throws Core_Exception 
     */
    public function addMapLayoutSection($mapParams, $redisOnly = false)
    {
        try
        {
            $insertRS = false;
            
            if($redisOnly === true)
            {
                $insertRS = true;
                $cacheFormat = $mapParams;
            }
            else
            {
                
                $maxOrder = $this->getMapLayoutSectionMaxOrder($mapParams);
            
                if($maxOrder != false)
                {
                    $mapParams['MapOrder'] = $maxOrder;
                    //init model metadata
                    $mapMeta = new Fpt_Business_Block_layout_metadata_maplayoutsection();
                    //init data
                    $mapMeta->init($mapParams);
                    //get formatted data
                    $formatParams = $mapMeta->getData();
                    //unsert old
                    unset($mapParams);
                    
                    //read config 
                    $config = Fpt_Global::getConfig('application');
                    //init model db
                    $modelSql = Fpt_Business_Block_layout::factory($config['block']['db']);
                    //add db layout
                    $insertRS = $modelSql->addMapLayoutSection($formatParams);
                    $cacheFormat = $mapMeta->getDataCache();
                }
            }
            //if insert db success;
            if($insertRS !== false)
            {
                //init model redis
                $layoutRedis = Fpt_Business_Block_layout_adapter_redis::getInstance();

                $redis =  $layoutRedis->addMapLayoutSection($cacheFormat);

                if($redis != false)
                {
                    return $cacheFormat;
                }
            }
            return false;
            
        }
        catch(Exception $ex)
        {
            throw new Core_Exception($ex->getMessage(), $ex->getCode());
        }
    }
    
    
    
    public function getMapLayoutSectionMaxOrder($mapParams)
    {
        try
        {
            //init model metadata
            $mapMeta = new Fpt_Business_Block_layout_metadata_maplayoutsection();
            //init data
            $mapMeta->init($mapParams);
            //get formatted data
            $formatParams = $mapMeta->getData();
            //init model redis
            $layoutRedis = Fpt_Business_Block_layout_adapter_redis::getInstance();
            $currentLength = $layoutRedis->getMapLayoutSectionMaxOrder($formatParams);
           
            $currentLength++;
            
            return $currentLength;
        }
        catch(Exception $ex)
        {
            throw new Core_Exception($ex->getMessage(), $ex->getCode());
        }
    }
    
    
    public function getMapLayoutSection($mapParams)
    {
        try
        {
            //init model metadata
            $mapMeta = new Fpt_Business_Block_layout_metadata_maplayoutsection();
            //init data
            $mapMeta->init($mapParams);
            //get formatted data
            $formatParams = $mapMeta->getData();
            //init model redis
            $layoutRedis = Fpt_Business_Block_layout_adapter_redis::getInstance();
            $response['data'] = $layoutRedis->getMapLayoutSection($formatParams);
            $response['key']  = $formatParams['MapName'];
            return $response;
        }
        catch(Exception $ex)
        {
            throw new Core_Exception($ex->getMessage(), $ex->getCode());
        }
    }
    
    public function addMapSectionBlock($mapParams, $redisOnly = false)
    {
        try
        {
            if($redisOnly === true)
            {
                $insertRS = true;
                $cacheFormat = $mapParams;
                $cacheFormat['MapParams'] = Zend_Json::encode($cacheFormat['MapParams']);
            }
            else
            {
                //init model metadata
                $mapMeta = new Fpt_Business_Block_layout_metadata_mapsectionblock();
                
                $maxOrder = $this->getMapSectionBlockMaxOrder($mapParams);
                
                
                if($maxOrder != false)
                {
                    $mapParams['MapOrder'] = $maxOrder;
                    //init data
                    $mapMeta->init($mapParams);
                    //get formatted data
                    $formatParams = $mapMeta->getData();
                    
                    //read config 
                    $config = Fpt_Global::getConfig('application');
                    //init model db
                    $modelSql = Fpt_Business_Block_layout::factory($config['block']['db']);
                    //add db layout
                    $insertRS = $modelSql->addMapSectionBlock($formatParams);
                    //get short
                    $cacheFormat = $mapMeta->getDataCache();
                }
                
            }
            //if insert db success;
            if($insertRS !== false)
            {
                
                //init model redis
                $layoutRedis = Fpt_Business_Block_layout_adapter_redis::getInstance();
  
                $redis =  $layoutRedis->addMapSectionBlock($cacheFormat);

                if($redis != false)
                {
                    return $cacheFormat['MapName'];
                }
            }
            return false;
        }
        catch(Exception $ex)
        {
            throw new Core_Exception($ex->getMessage(), $ex->getCode());
        }
    }
    
    public function removeMapSectionBlock($mapParams)
    {
        try
        {
            
            //init model metadata
            $mapMeta = new Fpt_Business_Block_layout_metadata_mapsectionblock();
            //init data
            $mapMeta->init($mapParams);
            //get formatted data
            $formatParams = $mapMeta->getData();
            
            
            $layoutMysql = Fpt_Business_Block_layout_adapter_mysql::getInstance();
            
            $layoutMysql->removeMapSectionBlock($formatParams);
            //init model redis
            $layoutRedis = Fpt_Business_Block_layout_adapter_redis::getInstance();
            $response = $layoutRedis->removeMapSectionBlock($formatParams);
            
            return $response;
        }
        catch(Exception $ex)
        {
            throw new Core_Exception($ex->getMessage(), $ex->getCode());
        }
    }
    
    public function editMapSectionBlock($mapParams)
    {
        try
        {
            
            //init model metadata
            $mapMeta = new Fpt_Business_Block_layout_metadata_mapsectionblock();
            //init data
            $mapMeta->init($mapParams);
            //get formatted data
            $formatParams = $mapMeta->getData();
            $layoutMysql = Fpt_Business_Block_layout_adapter_mysql::getInstance();
            
            $layoutMysql->updateMapSectionBlock($formatParams);
            //init model redis
            $layoutRedis = Fpt_Business_Block_layout_adapter_redis::getInstance();
            
            $response = $layoutRedis->editMapSectionBlock($formatParams);
            
            
            return $response;
        }
        catch(Exception $ex)
        {
            throw new Core_Exception($ex->getMessage(), $ex->getCode());
        }
    }
    
    public function getMapSectionBlock($mapParams)
    {
        try
        {
            //init model metadata
            $mapMeta = new Fpt_Business_Block_layout_metadata_mapsectionblock();
            //init data
            $mapMeta->init($mapParams);
            //get formatted data
            $formatParams = $mapMeta->getData();

            //init model redis
            $layoutRedis = Fpt_Business_Block_layout_adapter_redis::getInstance();

            $response['data'] = $layoutRedis->getMapSectionBlock($formatParams);
            
            if(!$response['data'])
            {
                $layoutMysql = Fpt_Business_Block_layout_adapter_mysql::getInstance();
            
                $resultMysql = $layoutMysql->getMapSectionBlock($formatParams);
                
                if(!empty($resultMysql))
                {
                    $mapBlockParams = array(
                        'MapLayoutSectionName'  => $resultMysql['MapLayoutSectionName'],
                        'SectionName'           => $resultMysql['SectionName'],
                        'SectionOrder'          => $mapParams['SectionOrder'],
                        'BlockName'             => $resultMysql['BlockName'],
                        'MapParams'             => $resultMysql['MapParams'],
                    );
                    $this->addMapSectionBlock($mapBlockParams, true);
                }
            }
            return $response;
        }
        catch(Exception $ex)
        {
            throw new Core_Exception($ex->getMessage(), $ex->getCode());
        }
    }
    
    
    public function getMapSectionBlockMaxOrder($mapParams)
    {
        try
        {
            //init model metadata
            $mapMeta = new Fpt_Business_Block_layout_metadata_mapsectionblock();
            //init data
            $mapMeta->init($mapParams);
            //get formatted data
            $formatParams = $mapMeta->getData();
            //init model redis
            $layoutRedis = Fpt_Business_Block_layout_adapter_redis::getInstance();
            $length = $layoutRedis->getMapSectionBlockMaxOrder($formatParams);
            $length++;
            return $length;
        }
        catch(Exception $ex)
        {
            throw new Core_Exception($ex->getMessage(), $ex->getCode());
        }
    }
    
    
    /**
     * mapBlockSection
     * @param array $params array('BlockName', 'BlockSection', 'BlockParams')
     * @return bool
     * @throws Core_Exception 
     */
    public function mapBlockSection($params, $mapBlockSection)
    {
        try
        {
            $layoutRedis = Fpt_Business_Block_redis_layout::getInstance();
            //get autoid temp 
            $mapID = intval($layoutRedis->getAutoID('block'));
            if($mapID > 0)
            {
                $params['MapID'] = $mapID;
            }
            return $layoutRedis->mapBlockSection($params, $mapBlockSection);
        }
        catch(Exception $ex)
        {
            throw new Core_Exception($ex->getMessage(), $ex->getCode());
        }
    }
    
    public function getMapBlockSection($params)
    {
        try
        {
            $layoutRedis = Fpt_Business_Block_redis_layout::getInstance();
           

            return $layoutRedis->getMapBlockSection($params);
        }
        catch(Exception $ex)
        {
            throw new Core_Exception($ex->getMessage(), $ex->getCode());
        }
    }
        
    public function editMapBlockSection($sectionName, $blockName, $order = 0, $blockParams = array())
    {
        try
        {
            $layoutRedis = Fpt_Business_Block_redis_layout::getInstance(); 
            return $layoutRedis->editMapBlockSection($sectionName, $blockName, $order, $blockParams);
        }
        catch(Exception $ex)
        {
            throw new Core_Exception($ex->getMessage(), $ex->getCode());
        }
    }
    
    public function removeMapBlockSection($sectionName, $blockName, $order=0)
    {
        try
        {
            $layoutRedis = Fpt_Business_Block_redis_layout::getInstance();
            
            return $layoutRedis->removeMapBlockSection($sectionName, $blockName, $order);
        }
        catch(Exception $ex)
        {
            throw new Core_Exception($ex->getMessage(), $ex->getCode());
        }
    }
    
   
    public function backupMap()
    {
        try
        {
            $layoutMysql = Fpt_Business_Block_layout_adapter_mysql::getInstance();
            
            $resultMysql = $layoutMysql->getMapLayoutSection();
           
            if(is_array($resultMysql))
            {
                foreach($resultMysql as $key => $mapDetail)
                {
                    $mapParams = array(
                        'LayoutName'    => $mapDetail['LayoutName'],
                        'SectionName'   => $mapDetail['SectionName'],
                        'Module'        => $mapDetail['Module'],
                        'Controller'    => $mapDetail['Controller'],
                        'Action'        => $mapDetail['Action'],
                        'CateID'        => $mapDetail['CateID'],
                        'MapName'       => $mapDetail['MapName'],
                        'MapOrder'      => $mapDetail['MapOrder'],
                        'MapParams'     => $mapDetail['MapParams']
                    );
                    $mapParams['MapLayout'] = $mapDetail['CateID'].'_'.$mapDetail['Module'];
                    $mapParams['MapLayout'].= '_'.$mapDetail['Controller'].'_'.$mapDetail['Action'];
                    $mapLayoutSectionParams = $this->addMapLayoutSection($mapParams, true);
                    
                }
            }
            $resultMysql = $layoutMysql->getMapSectionBlock();

            if(is_array($resultMysql))
            {
                foreach($resultMysql as $key=> $mapDetail)
                {
                    $mapBlockParams = array(
                        'MapLayoutSectionName' => $mapDetail['MapLayoutSectionName'],
                        'SectionName'       => $mapDetail['SectionName'],
                        'BlockName'         => $mapDetail['BlockName'],
                        'MapName'           => $mapDetail['MapName'],
                        'MapOrder'      => $mapDetail['MapOrder'],
                        'MapParams'     => Zend_Json::decode($mapDetail['MapParams']),
                    );
                    
                    $mapLayoutSectionParams = $this->addMapSectionBlock($mapBlockParams, true);
                 
                }
            }
        }
        catch(Exception $ex)
        {
            throw new Core_Exception($ex->getMessage(), $ex->getCode());
        }
    }
    
      
   
    /**
     * Add Block with default view
     * @param array $params
     * @return type
     * @throws Core_Exception 
     */
    public function addBlock($params)
    {
        try
        {
            $layoutRedis = Fpt_Business_Block_redis_layout::getInstance();
            //get autoid
            $blockID = intval($layoutRedis->getAutoID());
            if($blockID > 0)
            {
                $params['BlockID'] = $blockID;
            }
            return $layoutRedis->addBlock($params);
        }
        catch(Exception $ex)
        {
            throw new Core_Exception($ex->getMessage(), $ex->getCode());
        }
    }
    
    public function getBlockByNames($arrNames)
    {
        try
        {
            $layoutRedis = Fpt_Business_Block_redis_layout::getInstance();
            $rs = $layoutRedis->getBlockByNames($arrNames);
            foreach($rs as $name => $blockDetail)
            {
                $blockDetail = Zend_Json::decode($blockDetail);
                $rs[$name] = $blockDetail;
            }
            return $rs;
        }
        catch(Exception $ex)
        {
            throw new Core_Exception($ex->getMessage(), $ex->getCode());
        }
    }
    /**
     * add 1 section 
     * @param type $params
     * @param array $blockIDs
     * @return type
     * @throws Core_Exception 
     */
    public function addSection($params, $blockIDs = array())
    {
        try
        {
            $layoutRedis = Fpt_Business_Block_redis_layout::getInstance();
            //get autoid
            $sectionID = intval($layoutRedis->getAutoID());
            
            if($sectionID > 0)
            {
                $params['SectionID']          = $sectionID;
            }
            
            return $layoutRedis->addSection($params, $blockIDs);
        }
        catch(Exception $ex)
        {
            throw new Core_Exception($ex->getMessage(), $ex->getCode());
        }
    }
    

    /**
     * Get list section
     * @return type
     * @throws Core_Exception 
     */
    public function getListSection()
    {
        try
        {
            return Fpt_Business_Block_redis_layout::getInstance()->getListSection();
        }
        catch(Exception $ex)
        {
            throw new Core_Exception($ex->getMessage(), $ex->getCode());
        }
    }
    /**
     * get list blocks
     * @return type
     * @throws Core_Exception 
     */
    public function getListBlock()
    {
        try
        {
            return Fpt_Business_Block_redis_layout::getInstance()->getListBlock();
        }
        catch(Exception $ex)
        {
            throw new Core_Exception($ex->getMessage(), $ex->getCode());
        }
    }
}